<?php
 
	function smarty_block_supplieProducts($params, $content, &$smarty, &$repeat) {

		global $db, $config;

		$l = count($smarty->_tag_stack);
		if(empty($params['name'])) $params['name'] = "products";

		if(empty($smarty->blockvars[$l])) {
			$sqlstr = "select hsp.*,hps.pid,hps.pname,hps.spec,hps.pic,hps.status as pc_status,hps.ms_status from {$config->db_prefix}supplie_products AS hsp LEFT JOIN  haiou_product_sku AS hps ON hps.id=hsp.product_sku_id where 1=1";
			if (!empty($params['id'])) $sqlstr .= " and hsp.id = ".$db->qstr($params['id']);
			if (!empty($params['product_id'])) $sqlstr .= " and hps.pid in(".trim($params['product_id']).")";
			if (!empty($params['shopid'])) $sqlstr .= " and hsp.shopid = ".$db->qstr($params['shopid']);
			if (!empty($params['supplier_id'])) $sqlstr .= " and hsp.supplieid = ".$db->qstr($params['supplier_id']);
			
			if (isset($params['keywords']) && !empty($params['keywords'])){
				if(is_numeric($params['keywords'])){
					$sqlstr .= " and hps.pid like ". $db->qstr($params['keywords']."%");
				}else{
					$sqlstr .= " and hps.pname like ". $db->qstr("%".$params['keywords']."%");
				}
			}
			
			if ($params['lprice']!='') $sqlstr .= " and hsp.pricebuy>=". $db->qstr($params['lprice']);
			if ($params['hprice']!='') $sqlstr .= " and hsp.pricebuy<=". $db->qstr($params['hprice']);
			
			if ($params['lstock']!='') $sqlstr .= " and hsp.stock>=". $db->qstr($params['lstock']);
			if ($params['hstock']!='') $sqlstr .= " and hsp.stock<=". $db->qstr($params['hstock']);
			
			if (!empty($params['cond'])) $sqlstr .= " and ({$params['cond']})";
			if (!empty($params['order'])) $sqlstr .= " order by {$params['order']}";
			
			
			if (!empty($params['product_id'])) $params['limit']=0;
			$limit = empty($params['limit'])?9999:$params['limit'];
			$page = empty($params['page'])?1:$params['page'];
			$pager = new pager($limit, $page);
			$smarty->blockvars[$l] = $pager->execute($db, $sqlstr);

			//
			if(!$smarty->blockvars[$l]) return $repeat = false;
			if(!empty($params['pager'])) $smarty->assign($params['pager'], $pager);
			//取sku第一张图
			foreach ($smarty->blockvars[$l] as $kk=>$vv)
			{
				$smarty->blockvars[$l][$kk]['stocks'] = number_format($vv['stocks'],0);
				if(!($smarty->blockvars[$l][$kk]['product_pic'] = @strstr($vv['pic'], ",",true)))
				{
					$smarty->blockvars[$l][$kk]['product_pic'] = $vv['pic'];
				}
				if($vv['supplieid']!=""){
					$sql = "select * from {$config->db_prefix}suppliers where find_in_set(id,'{$vv['supplieid']}')";
					$suppliers = $db->GetArray($sql);
					$smarty->blockvars[$l][$kk]['suppliers'] = $suppliers;
				}else{
					$smarty->blockvars[$l][$kk]['suppliers'] = array();
				}
				if($vv['shopid']!=""){
					$sql = "select * from {$config->db_prefix}shops where id={$vv['shopid']} limit 1";
					$shops = $db->getrow($sql);
					$smarty->blockvars[$l][$kk]['shop_name'] = $shops['title'];
				}
			}
		}
	
		if(list($key, $item) = each($smarty->blockvars[$l])) {
			$repeat = true;
			$smarty->assign($params['name'], $item);
		}
		else {
			$repeat = false;
			reset($smarty->blockvars[$l]);
		}
		
		if(!is_null($content)) print $content;
	
		//
		if(!$repeat) $smarty->blockvars[$l] = array();
	}

?>